﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZJGIS.ManageUtility.Modules;
using ZJGIS.Util.Custom;
using ZJGIS.Util.Data;

namespace ZJGIS.ManageUtility
{
    /// <summary>
    /// 全局操作对象类
    /// </summary>
    public class ContextHelper
    {
        /// <summary>
        /// 维护库数据操作对象，在系统启动是复制
        /// </summary>
        public static IDbContext DBContext { get; set; }

        /// <summary>
        /// 超级管理员用户登录名称
        /// </summary>
        public const string Administrator = "Administrator";

        /// <summary>
        /// 功能权限配置主键
        /// </summary>
        public const string KEY_FUNCTION = "Function";

        /// <summary>
        /// TOC功能配置主键
        /// </summary>
        public const string KEY_TOC = "TOC";

        /// <summary>
        /// 数据权限配置主键
        /// </summary>
        public const string KEY_DATA = "Data";

        /// <summary>
        /// 范围权限配置主键
        /// </summary>
        public const string KEY_REGION = "Region";

        /// <summary>
        /// 权限配置
        /// </summary>
        public static PrivilegeConfig PrivilegeCfg { get; set; }

        /// <summary>
        /// 当前登录的用户
        /// </summary>
        public static UserInfo CurrentUser { get; set; }

        /// <summary>
        /// 系统所有的配置项
        /// </summary>
        public static List<Hash_Table> Hash_Tables { get; set; }

        /// <summary>
        /// 获取指定的配置项
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static Hash_Table GetHash_Table(string key)
        {
            if (Hash_Tables == null) Hash_Tables = new List<Hash_Table>();
            Hash_Table ht = Hash_Tables.FirstOrDefault(t => t.Key.Equals(key, StringComparison.CurrentCultureIgnoreCase));
            if (ht != null) return ht;
            ht = ContextHelper.DBContext.GetEntryByID<Hash_Table>(key, "key");
            if (ht == null) return null;
            Hash_Tables.Add(ht);
            return ht;
        }

        /// <summary>
        /// 
        /// </summary>
        public static bool RegionStarted
        {
            get
            {
                Hash_Table ht = GetHash_Table(KEY_REGION);
                if (ht == null) return false;
                string v = Encoding.UTF8.GetString(ht.Value);
                return bool.Parse(v);
            }
            set
            {
                Hash_Table ht = new Hash_Table()
                {
                    Key = KEY_REGION,
                    Value = System.Text.Encoding.UTF8.GetBytes(value.ToString())
                };
                bool isnew = false;
                if (ContextHelper.Hash_Tables == null ||
                    ContextHelper.Hash_Tables.Count == 0 ||
                    !ContextHelper.Hash_Tables.Exists(t => t.Key.Equals(KEY_REGION, StringComparison.CurrentCultureIgnoreCase)))
                    isnew = true;
                int rst = 0;
                if (isnew) rst = ContextHelper.DBContext.Insert<Hash_Table>(ht);
                else rst = ContextHelper.DBContext.Update<Hash_Table>(ht, "key", ht.Key);
                if (rst > 0 && !isnew)
                {
                    Hash_Table htext = Hash_Tables.FirstOrDefault(t => t.Key.Equals(ht.Key, StringComparison.CurrentCultureIgnoreCase));
                    if (htext != null)
                    {
                        htext.Value = ht.Value;
                    }
                }
            }
        }

    }
}
